#ifndef GB28181_PTZ_H
#define GB28181_PTZ_H

#include <string>
#include <functional>

extern "C" {
#include <eXosip2/eXosip.h>
#include <osipparser2/osip_message.h>
}

namespace El {
namespace GB28181 {

// Forward declarations
struct SipServerConfig;

// PTZ command types
enum class PTZCommand {
    UNKNOWN = 0,
    UP = 1,          // 上
    DOWN = 2,        // 下
    LEFT = 3,        // 左
    RIGHT = 4,       // 右
    UP_LEFT = 5,     // 左上
    UP_RIGHT = 6,    // 右上
    DOWN_LEFT = 7,   // 左下
    DOWN_RIGHT = 8,  // 右下
    ZOOM_IN = 9,     // 放大
    ZOOM_OUT = 10,   // 缩小
    STOP = 11,       // 停止
    PRESET_SET = 12, // 设置预置位
    PRESET_CALL = 13,// 调用预置位
    PRESET_DELETE = 14, // 删除预置位
    FOCUS_NEAR = 15, // 近焦点
    FOCUS_FAR = 16,  // 远焦点
    IRIS_OPEN = 17,  // 光圈开
    IRIS_CLOSE = 18  // 光圈关
};

// PTZ control info
struct PTZControlInfo {
    std::string device_id;
    std::string channel_id;
    PTZCommand command;
    int speed;        // 速度 (1-255)
    int preset_id;    // 预置位编号 (1-255)
    int zoom_level;   // 变倍级别
    
    PTZControlInfo() : command(PTZCommand::UNKNOWN), speed(64), preset_id(0), zoom_level(0) {}
};

// PTZ control callback
using PTZControlCallback = std::function<bool(const PTZControlInfo& info)>;

// PTZ control handler for GB28181
class GB28181PTZ {
public:
    // Process DeviceControl message for PTZ
    static void ProcessDeviceControl(eXosip_event_t *event, eXosip_t *exosip_context,
                                   const SipServerConfig &config,
                                   PTZControlCallback controlCallback);
    
    // Send DeviceControl response
    static void SendDeviceControlResponse(eXosip_t *exosip_context, eXosip_event_t *event,
                                        bool success, const std::string &error_msg = "");
    
    // Parse PTZ control XML
    static bool ParsePTZControlXML(const std::string &xml, PTZControlInfo &info);
    
    // Create PTZ control response XML
    static std::string CreatePTZControlResponseXML(const std::string &device_id, 
                                                  const std::string &sn, 
                                                  bool success,
                                                  const std::string &error_msg = "");
    
    // PTZ command to string
    static std::string PTZCommandToString(PTZCommand cmd);
    
    // String to PTZ command
    static PTZCommand StringToPTZCommand(const std::string &cmd_str);
    
    // Generate PTZ command string for different operations
    static std::string GeneratePTZCmdString(PTZCommand cmd, int speed, int preset_id = 0);
    
private:
    // Parse PTZ speed from XML
    static int ParsePTZSpeed(const std::string &xml);
    
    // Parse preset ID from XML
    static int ParsePresetId(const std::string &xml);
    
    // Extract XML value by tag
    static std::string ExtractXMLValue(const std::string &xml, const std::string &tag);
    
    // Validate PTZ parameters
    static bool ValidatePTZParams(const PTZControlInfo &info);
};

} // namespace GB28181
} // namespace El

#endif // GB28181_PTZ_H